package com.pw.study.tcp;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @Author: linux_future
 * @since: 2022/3/30
 **/
public class TcpServerV3 extends BaseServer {


    public static void main(String[] args) throws IOException {
        new TcpServerV3().init(6666);
    }


    @Override
    protected void msgHandler(InputStream in, OutputStream out) throws IOException {

        BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
        writer.write("server --> client ..\n");
        writer.flush();
        System.out.println("webapp:" + webApp.getRemoteSocketAddress());
        String doClient_msg = null;
        for (; ; ) {
            String str = reader.readLine();
            if (webApp == null) {
                System.out.println("webApp 还没启动。。。。");
                doClient_msg = "webApp 还没启动。。。。";
            } else {
                if (str.startsWith("http://")) {
                    doClient_msg = doClient(str);
                } else {
                    System.out.println("其他处理逻辑");
                    doClient_msg="其他处理逻辑";
                }
            }

            System.out.println("server receive:  " + str);
            if ("bye".equals(str)) {
                writer.write("bye client ...");
                writer.flush();
                break;
            }
            writer.write("ok: " + doClient_msg + " 数据处理");
            writer.flush();
            System.out.println("=====================");
        }
    }

    private String doClient(String str) {
        System.out.println("str:准备发送http请求：" + str);
        String msg = "webApp发起的请求。。。。。" + str;
        return msg;

    }
}
